/**
 * Created by Administrator on 2017/3/10.
 * 持久层--操作用户增删改查
 */
var OptPool = require("./OptPool");
var NoteBean = require("../model/NoteBean");
var async = require("async");

function NoteDao(){
    //查询所有记录
    this.selectAll = function (retList) {
        var sql = "select tid,content,font_size,font_style,bg_color,DATE_FORMAT(createtime,\"%Y-%m-%d %h:%i:%s\") as createtime  from t_love where state=1";
        function callback(conn) {
            conn.query(sql , function (err,_result) {
                if(err){
                    console.log("select all user error:"+err);
                    retList("selectAllNoteError", "select all user error:"+err );
                }else{
                    retList("selectAllNoteSuccess", _result);
                }
                conn.release();
            });
        }
        this.getConnection(callback);
    }
    //通过主键查询一条
    this.selectById = function (tid,retOne) {
        var sql = "select  tid,content,font_size,font_style,bg_color,search_count,DATE_FORMAT(createtime,\"%Y-%m-%d %h:%i:%s\") as createtime from t_love where tid=?";
        function callback(conn) {
            conn.query(sql , tid ,function (err,_result) {
                if(err || _result==null){
                    retOne("selectOneNoteError", "selectOneNoteError:"+err );
                }else{
                    retOne("selectOneNoteSuccess",_result );
                }
                conn.release();
            });
        }
        this.getConnection(callback);
    }
    
    this.insertNote = function (params , retMsg) {
        console.log(params);
            function callback(conn) {
                //使用瀑布流模式控制异步流程
                async.waterfall([
                    function(next){
                    //插入数据
                        var sql = "insert into  t_love  ("
                            + "createtime,"
                            + "content,"
                            + "bg_color,"
                            + "font_style,"
                            + "font_size,"
                            + "ipaddress ) value (NOW(),?,?,?,?,?)";
                        conn.query(sql ,params, function (err,_result) {
                            if(err){
                                next(null,err);
                            }else{
                                next(null,_result);
                            }
                        });
                    },
                    function(perVal,next){
                     //获取插入后的主键
                        sql = "SELECT LAST_INSERT_ID() as count";
                        conn.query(sql , function (err,_result) {
                            if(err){
                                next(null,err);
                            }else{
                                next(null,_result);
                            }
                        });
                    },

                ],function (err, data) {
                    if(err){
                        retMsg("addNoteError", "addNoteError:"+err );
                    }else{
                        retMsg("addNoteSuccess",(data[0].count)+"");
                    }
                    conn.release();
                });
            }
            this.getConnection(callback);
        }

    //获取连接用
    this.getConnection = function(callback){
        var optPool = new OptPool();
        var pool = optPool.getPool(); //初始化连接池
        pool.getConnection(function (err, conn) {
            callback(conn);  //回调函数
        });
    }
}

module.exports = NoteDao;